package com.atguigu.gulimall.product.app;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Classname CategoryBrandRelationController
 * @Description TODO
 * @Date 2021/8/5 17:21
 * @Author Anonymous
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    //  获取分类关联的品牌   /product/categorybrandrelation/brands/list  catId:分类id
//    @GetMapping(value = "/brands/list")
//    public R getCategoryRelationBrandsList(@RequestParam("catId") String catId) {
//        List<CategoryBrandRelationEntity> catelog_id = categoryBrandRelationService.getBaseMapper()
//                .selectList(new QueryWrapper<CategoryBrandRelationEntity>()
//                        .eq("catelog_id", catId));
//        return R.ok().put("data", catelog_id);
//    }

    //1、Controller：处理请求，接收和校验参数
    //2、Service：接收Controller传来的数据进行业务处理
    //3、Controller：接收Service处理完的数据，封装页面指定的vo
    @GetMapping(value = "/brands/list")
    public R getCategoryRelationBrandsList(@RequestParam(value = "catId", required = true) String catId) {
        //有时候返回的数据可能不止要brandId和brandName，还需要BrandEntity中的logo等信息，因此先返回BrandEntity
        List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> collect = vos.stream().map((item) -> {
            BrandVo brandVo = new BrandVo();
            //由于BrandEntity中的品牌名用的字段是name，而BrandVo中的品牌名用的是BrandName，属性名不同则不能使用Beanutils中的copyProperties方法进行属性对拷，只有手动set进去
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    //获取品牌关联的分类
    @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
    public Map<String, Object> list(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().
                        eq("brand_id", brandId)
        );
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        return map;
//        return R.ok().put("data",data);
    }

    //新增品牌与分类关联关系
//    @RequestMapping(value = "/save",method = RequestMethod.POST)
//    public R save(@RequestParam("brandId") Long brandId, @RequestParam("catelogId") Long catelogId) {
//        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
//        categoryBrandRelationEntity.setBrandId(brandId);
//        categoryBrandRelationEntity.setCatelogId(catelogId);
//        categoryBrandRelation.save(categoryBrandRelationEntity);
//        return R.ok();
//    }
    //新增品牌与分类关联关系,由于前端没有传入brand_name,catelog_name字段，因此不用原生的save直接保存，稍微修改一下
    //如果直接调用save保存的话，数据库中的这两个字段就为空了
    @PostMapping(value = "/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }
}
